﻿using BLL.BarCode;
using BLL.custom;
using BLL.sys;
using Entity;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Web;
using System.Web.Mvc;
using Tools;

namespace WebProject.Controllers
{
    public class ServiceController : Controller
    {
        ReturnResult rsl;
        TxtLogHelper txt;
        BLL.BarBLL barBLL;
        ServiceBLL serBLL;

        CuDbHelper db;

        public ServiceController()
        {
            txt = new TxtLogHelper();
            barBLL = new BLL.BarBLL();
            serBLL = new ServiceBLL();

            db = new CuDbHelper();
        }
        /// <summary>
        /// 条码保存接口
        /// </summary>
        /// <param name="datas"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult SaveBarCode()
        {
            rsl = new ReturnResult();
            try
            {
                Stream reqStream = Request.InputStream;
                StreamReader streamReader = new StreamReader(reqStream);
                var json = streamReader.ReadToEnd();
                if (string.IsNullOrWhiteSpace(json))
                {
                    rsl.Message = "无法获取传递参数"; return Json(rsl);
                }
                else
                {
                    txt.AddLogMsg("调用接口SaveBarCode，入参参数:" + json);
                    rsl = barBLL.SaveBarCode(json);
                }
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return Json(rsl);
        }


        /// <summary>
        /// 根据条码查询条码是否合法
        /// </summary>
        /// <param name="barCode"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult getProductInfoByBarCode(string barCode)
        {
            return Json(barBLL.getProductInfoByBarCode(barCode));
        }

        /// <summary>
        /// 获取有效生产岗位
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult getPositionInfo()
        {
            rsl = new ReturnResult();
            try
            {
                Stream reqStream = Request.InputStream;
                StreamReader streamReader = new StreamReader(reqStream);
                var json = streamReader.ReadToEnd();
                if (string.IsNullOrWhiteSpace(json))
                {
                    rsl.Message = "无法获取传递参数"; return Json(rsl);
                }
                else
                {
                    txt.AddLogMsg("调用接口getPositionInfo，入参参数:" + json);

                    rsl = serBLL.getPositionInfo(json);
                }
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return Json(rsl);
        }
        [HttpPost]
        public JsonResult ProductWage()
        {
            rsl = new ReturnResult();
            try
            {
                Stream reqStream = Request.InputStream;
                StreamReader streamReader = new StreamReader(reqStream);
                var json = streamReader.ReadToEnd();
                if (string.IsNullOrWhiteSpace(json))
                {
                    rsl.Message = "无法获取传递参数"; return Json(rsl);
                }
                else
                {
                    txt.AddLogMsg("调用接口ProductWage，入参参数:" + json);

                }
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return Json(rsl);
        }
      
        /// <summary>
        /// 条码作废
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult DropBaCodeInfo()
        {
            rsl = new ReturnResult();
            try
            {
                Stream reqStream = Request.InputStream;
                StreamReader streamReader = new StreamReader(reqStream);
                var json = streamReader.ReadToEnd();
                if (string.IsNullOrWhiteSpace(json))
                {
                    rsl.Message = "无法获取传递参数"; return Json(rsl);
                }
                else
                {

                    txt.AddLogMsg("调用接口DropBaCodeInfo，入参参数:" + json);
                    var obj = JsonConvert.DeserializeObject<JObject>(json);
                    rsl = barBLL.DropBarCode(obj["barCode"].ToString(), obj["userCode"].ToString());
                }
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return Json(rsl);
        }

        // 生产打码
        // 查询商标,包装方式 (参数：销售订单号，子表行号)
        // 查询箱规 （参数：存货编码）
        [HttpPost]
        public JsonResult GetTradeMark_Packing_BoxSpec()
        {
            Stream reqStream = Request.InputStream;
            StreamReader streamReader = new StreamReader(reqStream);
            var json = streamReader.ReadToEnd();

            return Json(serBLL.GetTradeMark_Packing_BoxSpec(json));
        }

        [HttpGet]
        public FileResult GetLatestPrintClient()
        {
            var currentDirectory = System.Web.HttpContext.Current.Request.MapPath("/");
            var updatesFolder = currentDirectory + "\\" + "Updates";
            // 调试地址
            //http://localhost:2824/service/GetLatestPrintClient
            if (!Directory.Exists(updatesFolder))
            {
                Directory.CreateDirectory(updatesFolder);
            }

            var versionNames = Directory.GetDirectories(updatesFolder);

            var latestVersion = versionNames.Max();

            if (latestVersion != null)
            {
                var programFilePath = latestVersion + "\\" + "pTools.exe";
                var programFile = System.IO.File.ReadAllBytes(programFilePath);
                var ms = new MemoryStream(programFile);
                return File(ms, "application/vnd.microsoft.portable-executable", "pTools.exe");
            }
            else
            {
                return null;
            }
        }
        [HttpGet]
        public string GetLastVersionString()
        {
            var currentDirectory = System.Web.HttpContext.Current.Request.MapPath("/");
            var updatesFolder = currentDirectory + "\\" + "Updates";
            // 调试地址
            //http://localhost:2824/service/GetLatestPrintClient
            if (!Directory.Exists(updatesFolder))
            {
                Directory.CreateDirectory(updatesFolder);
            }

            var versionNames = Directory.GetDirectories(updatesFolder);

            var latestVersion = versionNames.Max();

            latestVersion = Path.GetFileName(latestVersion);

            return latestVersion;
        }
        [HttpGet]
        public FileResult GetTemplateFileZip()
        {
            var currentDirectory = System.Web.HttpContext.Current.Request.MapPath("/");

            // 模板文件夹
            var templateFolder = currentDirectory + "\\" + "Templates";
            if (!Directory.Exists(templateFolder))
            {
                Directory.CreateDirectory(templateFolder);
            }

            // 存放压缩文件的临时文件夹
            var zipFolder = currentDirectory + "\\" + "Zips";
            if (!Directory.Exists(zipFolder))
            {
                Directory.CreateDirectory(zipFolder);
            }

            // 创建的压缩文件名
            var fileName = zipFolder + "\\" + "Templates_" + DateTime.Now.ToString("yyyyMMdd_hhmmss_fff") + ".zip";

            ZipFile.CreateFromDirectory(templateFolder, fileName);

            var zipFile = System.IO.File.ReadAllBytes(fileName);
            var ms = new MemoryStream(zipFile);
            return File(ms, "application/zip", "Templates.zip");
        }
        [HttpPost]
        public JsonResult MachineRecords()
        {
            rsl = new ReturnResult();
            try
            {
                Stream reqStream = Request.InputStream;
                StreamReader streamReader = new StreamReader(reqStream);
                var json = streamReader.ReadToEnd();
                if (string.IsNullOrWhiteSpace(json))
                {
                    rsl.Message = "无法获取传递参数"; return Json(rsl);
                }
                else
                {
                    rsl = serBLL.MarchineRecords(json);
                    txt.AddLogMsg("machineRecords，入参参数:" + json);
                }
            }
            catch (Exception ex)
            {
                rsl.Message = ex.Message;
            }
            return Json(rsl);
        }
    }
}